﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Windows.Controls;
using System.Windows.Input;
using FinPlusInterfaces;
using FinPlusAssembler;

namespace FinPlusCompView
{
    public class FinPlusControl : UserControl, IFinPlusComp
    {
        public virtual bool IsValid { get; set; }
        public virtual string Id { get; set; }
        public virtual string LinkedId { get; set; }
        public virtual string Config { get { return _config; } set { _config = string.IsNullOrEmpty(_config) ? value : _config; } }
        public virtual Env Env { get { return _env; } set { _env = _env == Env.unknown ? value : _env; } }
        public virtual IPods Adapter { get { return null; } }
        public virtual string TestString { get { return string.Empty; } }
        public string User { get { return _user ?? (_user = Environment.UserName); } }
        public IPods Commands { get { return _commands; } set { _commands = value; } }
        public IPods Structure { get { return _structure  ?? (_structure = GetStructure()); } }
        public IPPodManager Permissions
        {
            get
            {
                if (_permissionManager == null)
                    _permissionManager = IOC.Singleton<IPPodManager>("ClientConfig", Config, Env);
                return _permissionManager;
            }
        }
        public IAuditManager Audits
        {
            get
            {
                if (_auditManager == null)
                    _auditManager = IOC.Singleton<IAuditManager>("ClientConfig", User, Config, Env);
                return _auditManager;
            }
        }
        public readonly static RoutedCommand CmdRoute = new RoutedCommand();
        
        private volatile static IPods _structure;
        private volatile static Object _lock = new Object();
        private static IPPodManager _permissionManager;
        private static IAuditManager _auditManager;
        private static IPods _commands;
        private static Env _env = Env.unknown;
        private static string _user, _config;
        
        //construct
        public FinPlusControl() { }

        //common control interface
        public virtual void Dock(object control, string dock) 
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        public virtual void CmdRouting(object s, ExecutedRoutedEventArgs a)
        {
            try
            {
                CmdRouter(a.Parameter.ToString(), s);
            }
            catch (CmdRouterException e)
            {
                IsValid = Level.Error.Log(Id, Config, s.ToString(), e);
            }
        }

        public virtual void CmdRouter(string command, object control)
        {
            try
            {
                var tag = ((Control)control).Tag;
                CmdExecute(IOC.New<IPod>(command, Id, User, -1, tag == null ? string.Empty : tag.ToString()));
            }
            catch (CmdRouterException e)
            {
                IsValid = Level.Error.Log(Id, Config, command, e);
            }
        }

        public virtual void CmdRouter(string command, string tag)
        {
            try
            {
                CmdExecute(IOC.New<IPod>(command, Id, User, -1, tag));
            }
            catch (CmdRouterException e)
            {
                IsValid = Level.Error.Log(Id, Config, command, e);
            }
        }

        public virtual void CmdRouter(string command)
        {
            try
            {
                CmdExecute(IOC.New<IPod>(command, Id, User));
            }
            catch (CmdRouterException e)
            {
                IsValid = Level.Error.Log(Id, Config, command, e);
            }
        }

        public virtual void CmdExecute(IPod command) 
        {
            IsValid = false;
            throw new NotImplementedException();
        }
        
        public virtual void Dispose() 
        {
            DI.Dispose(LinkedId);
        }

        //public
        public UserControl DockSet(UserControl control, string dock)
        {
            DockPanel.SetDock((UserControl)control, dock.EnumParse<Dock>(System.Windows.Controls.Dock.Top));
            return control;
        }

        public IPods Children(UIElementCollection elements)
        {
            var pods =  IOC.New<IPods>();
            foreach (var child in elements)
            {
                var value = IOC.New<IPod>();
                value.Object = child;
                pods.Add(value);
            }

            return pods;
        }

        public void CmdCapture(IPod command)
        {
            if (_commands == null || command["_CommandLogged"].ToString().Equals(true.ToString()))
                return;

            _commands.Add(command);
            command["_CommandLogged"] = true;
        }

        //private
        private IPods GetStructure()
        {
            if (_structure == null)
                lock (_lock)
                    if (_structure == null)
                    {
                        var conn = new FinPlusConnectService("Structure", "Query", "[group],Structure;[type],Select;[where],", string.Empty, false);
                        _structure = conn.Adapter;
                        conn.Dispose();
                    }
            return _structure;
        }
    }
}


































































































































//Copyright2013<jnrton>AllRightsReserved 
//Contact j.nrton@finpus